/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.BillDetail;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Trung Duc
 */
public class BillDetailTableModel extends AbstractTableModel{
    List<BillDetail> listBillDetail;
    ResourceBundle messages;

    public BillDetailTableModel(ResourceBundle messages) {
        this.messages = messages;
        this.listBillDetail = new  ArrayList<BillDetail>();
    }
    
    public boolean AddBillDetail(BillDetail bd){
        for(BillDetail billDetail : listBillDetail){
            if(billDetail.getCategoryName().equals(bd.getCategoryName()) && billDetail.getItemName().equals(bd.getItemName()))
            {
                billDetail.setQuantity(billDetail.getQuantity()+bd.getQuantity());
                return false;
            }
        }
       return listBillDetail.add(bd);
    }
    public void removeBillDetail(int index){
        listBillDetail.remove(index);
    }
    public BillDetailTableModel(List<BillDetail> listBillDetail, ResourceBundle messages) {
        this.listBillDetail = listBillDetail;
        this.messages = messages;
    }

    public List<BillDetail> getListBillDetail() {
        return listBillDetail;
    }
    
    @Override
    public int getRowCount() {
        return listBillDetail.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        BillDetail billDetail = listBillDetail.get(rowIndex);
        switch(columnIndex){
            case 0: return rowIndex+1;
            case 1: return billDetail.getItemName();
            case 2: return billDetail.getCategoryName();
            case 3: return billDetail.getQuantity();
            case 4: return billDetail.getPrice();
            default: return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch(column){
            case 0: return messages.getString("order");
            case 1: return messages.getString("itemName");
            case 2: return messages.getString("categoryNameModel");
            case 3: return messages.getString("itemQuantity");
            case 4: return messages.getString("itemPrice");
            default: return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch(columnIndex){
            case 0: return Integer.class;
            case 1: return String.class;
            case 2: return String.class;
            case 3: return Integer.class;
            case 4: return Float.class;
            default: return null;
        }
    }
    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }
    
    public List<BillDetail> getBillDetailByBillID(int billID){
        List<BillDetail> list = new ArrayList<BillDetail>();
        for (Iterator<BillDetail> it = listBillDetail.iterator(); it.hasNext();) {
            BillDetail billDetail = it.next();
            if(billDetail.getBillID() == billID){
                list.add(billDetail);
            }
        }
        return list;
    }
}
